<?php
namespace Core\Uses;

function getErrors($erros = NULL) {

	\Core\Libs\Handler\Error::getErrors($erros);
}

function throwError() {

	\Core\Libs\Handler\Error::throwError();
}

function setSeverity($severity = 1) {

	\Core\Libs\Handler\Error::setSeverity($severity);
}

function getSeverity() {

	return \Core\Libs\Handler\Error::getSeverity();
}

function getSeverityAsString() {

	return \Core\Libs\Handler\Error::getSeverityAsString();
}

function getInternalError() {

	return \Core\Libs\Handler\Error::getInternalError();
}

function getErrorLine() {

	return \Core\Libs\Handler\Error::getErrorLine();
}

function getErrorFile() {

	return \Core\Libs\Handler\Error::getErrorFile();
}

function exception($mensagem = '', $codigo = NULL, $severity = 1, $file = NULL, $line = NULL, $internal = NULL, $previous = NULL, $chamada = 'Core\Uses\Exception') {

	if(func_num_args() > 0) {

		\Core\Libs\Handler\Error::throwErrors(&$mensagem, &$codigo, &$severity, &$file, &$line, &$internal, &$previous, &$chamada);
	}

	else {

		return \Core\Libs\Handler\Error::getInstance();
	}
}

function suppressErrors() {

	\Core\Libs\Handler\Error::suppressErrors();
}

function &shutdown() {

	\Core\Libs\Handler\Error::shutdown();
}

function getAlgorithmToHash() {

	return \Core\Libs\Crypt\Crypt::getAlgorithmToHash();
}

function restoreAlgorithmToHash() {

	\Core\Libs\Crypt\Crypt::restoreAlgorithmToHash();
}

function import() {

	
}

function getHash() {

	return call_user_func_array(array('\Core\Libs\Crypt\Crypt', 'getHash'), func_get_args());
}

function &uidGeneric($entropy = 3, $decimalPlaces = 3) {

	if($entropy <= 1) {

		return str_replace('.', '', uniqid());
	}

	elseif($entropy == 2) {

		return str_replace('.', '', uniqid(NULL, TRUE));
	}

	elseif($entropy == 3) {

		return str_replace('.', '', uniqid(NULL, TRUE) . mt_rand((int)'1' . str_repeat(0, $decimalPlaces), (int)'9' . str_repeat(9, $decimalPlaces)));
	}

	else {

		return str_replace('.', '', uniqid(mt_rand(), TRUE));
	}
}

function uuidOfElement() {

	return \Core\Uses\uidGeneric(3, 3);
}

function uuidGeneric($length = 36, $maxRand = FALSE) {

	$pid =& getmypid();

	if(strlen($pid) > 5) {

		$pid = mt_rand(10000, 99999);
	}

	$hex = \Core\Uses\uidGeneric(2) . base_convert(crc32(SERVER_SECURITY_TOKEN . $pid), 10, 16);
	$decimalPlaces = ($length - 4) - strlen($hex);

	if($decimalPlaces > 0) {

		$min = (int)'1' . str_repeat(0, $decimalPlaces - 1);
		$max = (int)'9' . str_repeat(9, $decimalPlaces - 1);
		$maxLength = FALSE;
		$mtMax = mt_getrandmax();

		if($max > $mtMax) {

			if($maxRand) {

				$min = 0;
			}

			else {

				$min = (int)'1' . str_repeat(0, strlen($mtMax) - 1);
			}

			$max = $mtMax;
			$hex .= mt_rand($min, $max);
			$maxLength =& strlen(&$hex);
		}

		else {

			$hex .= mt_rand($min, $max);
		}

		if($maxLength) {

			$hex .= str_repeat(0, ($length - $maxLength) - 4);
		}
	}

	return preg_replace('/(.{8})(.{4})(.{4})(.{4})(.+)/', '\1-\2-\3-\4-\5', &$hex);
}

function uuid() {

	$no = getenv('SERVER_ADDR');
	$pid = NULL;

	if(strpos(&$no, ':') !== FALSE) {

		if(substr_count(&$no, '::')) {

			$no =& str_replace('::', str_repeat(':0000', 8 - substr_count(&$no, ':')) . ':', &$no);
		}

		$no =& explode(':', &$no) ;
		$ipv6 = '' ;

		foreach($no as $id) {

			$ipv6 .= str_pad(base_convert($id, 16, 2), 16, 0, STR_PAD_LEFT);
		}

		$no =& base_convert(&$ipv6, 2, 10);

		if (strlen(&$no) < 38) {

			$no = NULL;
		}

		else {

			$no =& crc32(&$no);
		}
	}

	elseif(empty($no)) {

		$host = getenv('HTTP_HOST');

		if(!empty($host)) {

			$ip = gethostbyname($host);

			if($ip === $host) {

				$no =& crc32(&$host);
			}

			else {

				$no =& ip2long(&$ip);
			}
		}
	}

	elseif($no !== '127.0.0.1') {

		$no =& ip2long(&$no);
	}

	else {

		$no = NULL;
	}

	if(empty($no)) {

		$no =& crc32(SERVER_SECURITY_TOKEN);
	}

	if(function_exists('zend_thread_id')) {

		$pid =& zend_get_id();
	}

	else {

		$pid =& getmypid();
	}

	if(!$pid || $pid > 65535) {

		$pid = mt_rand(0, 0xfff) | 0x4000;
	}

	list($tempoMd, $tempoPq) = explode(' ', microtime());

	$uuid =& sprintf('%08x-%04x-%04x-%02x%02x-%04x%08x', (int)$tempoPq, (int)substr($tempoMd, 2) & 0xffff, mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3f) | 0x80, mt_rand(0, 0xff), &$pid, &$no);

	return $uuid;
}

function &ChecarCodificacaoInterna() {

	try {

		mb_internal_encoding(\Core\Uses\Ler('Core.codificacao.interna'));
	}

	catch(\Exception $e) {

		echo 'Codifica��o interna do PHP � inv�lida, favor rever as configura��es.';
		exit();
	}
}

function &getBrowserConfig() {

	$match = array();

	if(preg_match('|MSIE(\ ?)([0-9].[0-9]{1,2})|', HTTP_USER_AGENT, $match)) {

		define('_BROWSER', 'IE');
		define('_BROWSER_VERSION', $match[2]);
	}

	elseif(preg_match('|(Opera/([0-9]+.[0-9]{1,2})(.*Version/([0-9]+.[0-9]{1,2}))?)|', HTTP_USER_AGENT, $match)) {

		define('_BROWSER', 'Opera');
		(!empty($match[4])) ? define('_BROWSER_VERSION', $match[4]) : define('_BROWSER_VERSION', $match[2]);
	}

	elseif(preg_match('|Firefox/([0-9\.]+)|', HTTP_USER_AGENT, $match)) {

		define('_BROWSER', 'Firefox');
		define('_BROWSER_VERSION', $match[1]);
	}

	elseif(preg_match('|Chrome/([0-9\.]+)|', HTTP_USER_AGENT, $match)) {

		define('_BROWSER', 'Chrome');
		define('_BROWSER_VERSION', $match[1]);
	}

	elseif(preg_match('|(Version/([0-9\.]+).*)?Safari/([0-9\.]+)|', HTTP_USER_AGENT, $match)) {

		define('_BROWSER', 'Safari');
		(!empty($match[2])) ? define('_BROWSER_VERSION', $match[2]) : define('_BROWSER_VERSION', $match[3]);
	}

	else {

		$arrayBrowserConfig[0] = 'Other';
		$arrayBrowserConfig[1] = 0;
	}

	unset($match);
}

function &getBrowser() {

	if(defined('_BROWSER')) {

		return _BROWSER;
	}

	else {

		\Core\Uses\getBrowserConfig();
		return _BROWSER;
		
	}
}

function &getBrowserVersion() {

	if(defined('_BROWSER_VERSION')) {

		return _BROWSER_VERSION;
	}

	else {

		\Core\Uses\getBrowserConfig();
		return _BROWSER_VERSION;
		
	}
}

function &isContentCompression() {

	if(defined('_IS_CONTENT_COMPRESSION')) {

		return _IS_CONTENT_COMPRESSION;
	}

	$browser =& \Core\Uses\getBrowser();
	$version =& \Core\Uses\getBrowserVersion();

	if(($browser === 'Other' || $browser === 'IE' && $version < 7) && !function_exists('ob_gzhandler') || stripos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === FALSE) {

		define('_IS_CONTENT_COMPRESSION', FALSE);
		return FALSE;
	}

	define('_IS_CONTENT_COMPRESSION', TRUE);
	return TRUE;
}

function Closure($function) {

	return new \Core\Libs\Closure\Closure(&$function);
}

function &addSlashes($string, $arrayStripChars = array(), $metacaracteres = TRUE) {

	if($metacaracteres && $arrayStripChars !== FALSE) {

		$pattern = '(\ |\.|\,|\<|\>|\(|\)|\[|\]|\*|\^|\'|\"|\$|\~|\%|\#|\@|\{|\}|\!|\:|\?|\+|\=|\&|\||\\\\)';

		if(is_array(&$arrayStripChars) && count($arrayStripChars) > 0) {

			$functionFormat = function() use (&$arrayStripChars, &$pattern) {

				foreach($arrayStripChars as &$char) {

					$pattern =& preg_replace('/(\|)?(?(1)\\\\\\'.$char.'|\\\\\\'.$char.'(\|)?)/', '', &$pattern);
				}

				return $pattern;
			};

			$pattern =& $functionFormat();
		}

		unset($functionFormat, $arrayStripChars);
		return preg_replace('/'.$pattern.'/', '\\\\\1', &$string);
	}

	return addslashes(&$string);
}

function Gravar($parametro, $valor, $tipo = 'publico', $array = TRUE) {

	\Core\Libs\Extend\Config::gravar($parametro, $valor, $tipo, $array);
}

function Ler($parametro, $array = TRUE, $retorno = 'valor') {

	return \Core\Libs\Extend\Config::ler($parametro, $array, $retorno);
}

function &MemoriaUtilizada($real = FALSE) {

	$tamanho =& memory_get_usage(&$real);
	$unit = array('Bytes', 'Kylobytes', 'Megabytes', 'Gigabytes', 'Terabytes', 'Petabytes');
    return @round($tamanho/pow(1024,($i=floor(log($tamanho,1024)))),2).' '.$unit[$i];
}

function IncluirArquivo($parametro, $validar = FALSE) {

	if($validar && \Core\Uses\ValidarInclusaoArquivo(&$parametro, get_included_files())) {

		try {

			include($parametro);
		}

		catch(\Exception $e) {

			throw \Core\Uses\Exception('Erro: Arquivo não encontrado.');
		}
	}

	elseif(!in_array($parametro, get_included_files())) {

		try {

			include($parametro);
		}

		catch(\Exception $e) {

			throw \Core\Uses\Exception('Erro: Arquivo não encontrado.');
		}
	}
}

function &ChecarArquivo($parametro) {

	if(file_exists(&$parametro) && is_file(&$parametro) && !is_executable(&$parametro) && is_readable(&$parametro)) {

		return TRUE;
	}

	return FALSE;
}

function ValidarInclusaoArquivo($parametro, $arrayIncludes = array()) {

	if(!in_array(&$parametro, &$arrayIncludes)) {

		if(file_exists(&$parametro) && is_file(&$parametro) && !is_executable(&$parametro) && is_readable(&$parametro)) {

			return TRUE;
		}

		return FALSE;
	}
}

function InicializarDiretorios($app, $access = 'publico') {
	
	\Core\Libs\Extend\Directory::initDirectories($app, $access);
}

function dump($parametro) {

	//\Core\Uses\Gravar('Core.debug', 1);
	echo '<br><br><br>';
	echo var_dump($parametro);
	echo '<br><br><br>';
}
function &SetArrayParametros($array, $arrayPadrao) {

	foreach($array as $i => $valor) {

		if(array_key_exists(&$i, &$arrayPadrao)) {

			$arrayPadrao[$i] = $valor;
		}
	}

	return $arrayPadrao;
}
namespace Core\System;

use Core\Uses as System;
?>